package com.mrper.util;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;

/**
 * Created by admin on 2016/3/8.
 * 获取设备相关的辅助方法
 */
public class DeviceUtil {

    /**
     * 获取设备屏幕信息
     * @param context
     * @return 屏幕信息
     */
    public static DisplayMetrics getScreenInfo(Context context){
        DisplayMetrics displayMetrics = new DisplayMetrics();
        WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics;
    }

    /**
     * 获取设备屏幕宽度
     * @param context
     * @return 屏幕宽度
     */
    public static int getScreenWidth(Context context){
        return getScreenInfo(context).widthPixels;
    }

    /**
     * 获取设别屏幕高度
     * @param context
     * @return 屏幕高度
     */
    public static int getScreenHeight(Context context){
        return getScreenInfo(context).heightPixels;
    }

    /**
     * 获取电话号码
     * @param context
     * @return 电话号码
     */
    public static String getPhoneNumber(Context context){
        return ((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE)).getLine1Number();
    }

    /**
     * 获取设备的IMEI，某些设备不能获取成功
     * @param context
     * @return 设备的IMEI
     */
    public static String getPhoneIMEI(Context context){
        return ((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
    }

    /**
     * 获取设备的IMSI，某些设备不能获取成功
     * @param context
     * @return 设备的IMSI
     */
    public static String getPhoneIMSI(Context context){
        return ((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE)).getSubscriberId();
    }

    /**
     * 获取状态栏高度
     * @param context
     * @return 状态栏高度
     */
    public static int getStatusBarHeight(Context context){
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0){
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    /**  设置沉浸式状态栏  **/
    @TargetApi(19)
    public static void setTranslucentStatusBar(Activity activity,boolean on) {
        Window win = activity.getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on)
            winParams.flags =  winParams.flags | bits;
        else
            winParams.flags &= ~bits;
        win.setAttributes(winParams);
    }


}
